<template>
	<view class="my-settle">
		<!-- 全选 -->
		<label class="radio" @click="allGoodsCheck">
			<radio :checked="isAllCheck" color="#C00000"/><text>全选</text>
		</label>
		
		<!-- 合计 -->
		<view class="amount-box">
			合计：<text class="amount">￥{{checkedGoodsAmount}}</text>
		</view>
		<!-- 结算 -->
		<view class="btn-settle" @click="setllement">结算({{checkedCount}})</view>
	</view>
</template>

<script>
	import {mapGetters,mapMutations,mapState} from 'vuex'

	export default {
		name:"my-settle",
		computed:{
			...mapGetters('m_cart',['checkedCount','total','checkedGoodsAmount']),
			...mapGetters('m_user',['addStr']),
			...mapState('m_user',['token']),
			...mapState('m_cart',['cart']),
			isAllCheck(){
				console.log('1111',this.checkedCount === this.total)
				return this.checkedCount === this.total
			}
		},
		data() {
			return {
				second: 3,
				timer: null
			};
		},
		methods: {
			...mapMutations('m_cart',['updateAllGoodsState']),
			...mapMutations('m_user',['updateRedirectInfo']),
			allGoodsCheck() {
				console.log(!this.isAllCheck)
				this.updateAllGoodsState(!this.isAllCheck)
			},
			//用户点击了结算按钮
			setllement() { 
				if (!this.checkedCount) return uni.$showErrMsg('请选择结算商品')
				if (!this.addStr) return uni.$showErrMsg('请选择收货地址')
				// if (!this.token) return uni.$showErrMsg('请先登录')
				if (!this.token) return this.delayNavigate()
				this.payOrder()
			},
			async payOrder() {
				 // 1. 创建订单
				  // 1.1 组织订单的信息对象
				const orderinfo = {
					order_price : this.checkedGoodsAmount,
					// order_price: 0.01,
					consignee_addr: this.addStr,
					
					goods: this.cart.filter(x => x.goods_state).map(x =>({
						goods_id: x.goods_id,
						goods_number: x.goods_count,
						goods_price: x.goods_price
					})) 
				}
				// 1.2 发起请求创建订单
				const {data: res} = await uni.$http.post('/my/orders/create',orderinfo)
				if (res.meta.status !== 200) return uni.$showErrMsg('创建订单失败！')
				//  // 1.3 得到服务器响应的“订单编号”
				const orderNumber = res.message.number
				//  // 2. 订单预支付（获取支付参数）
				const {data : res1} = await uni.$http.post('/my/orders/req_unifiedorder',{order_number:'orderNumber'})
				if (res1.meta.status !== 200) return uni.$showErrMsg('预付订单生成失败！')
				const payinfo = res1.message.pay
				// 3. 发起微信支付
				// uni.requestPayment是一个统一各平台的客户端支付API，不管是在某家小程序还是在App中，客户端均使用本API调用支付。
				// 3.1 调用 uni.requestPayment() 发起微信支付
				const [err,success] = uni.requestPayment(payinfo)
				const [err, succ] = await uni.requestPayment(orderinfo)
				console.log(err,succ)
				//  3.2 未完成支付
				if(err) return uni.$showErrMsg('支付失败')
				// 3.3 完成了支付，进一步查询支付的结果
				const {data: res2} = await uni.$http.post('/my/orders/chkOrder',{order_number:'000'})
				// 3.4 检测到订单未支付 
				if(res2.meta.status !== 200) return uni.$showErrMsg('订单未支付')
				// 3.5 检测到订单支付完成
				 uni.showToast({
				 	title: '订单支付成功',
					icon: 'success'
				 })
			},
			delayNavigate(){
				this.second = 3//防止第2 3 n次 倒计时失败
				this.showTips(this.second)
			    this.timer = setInterval(()=>{
					this.second--
					if(this.second <= 0) {
						clearInterval(this.timer)
						
						uni.switchTab({
							url:'/pages/my/my',
							success:()=> {
								//登录成功重新返回购物车页面
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
						return 
					}
					this.showTips(this.second)
				},1000)
			},
			//展示倒计时的提示消息
			showTips(n){
				uni.showToast({
					icon: 'none',
					title: '请先登录再结算!'+ n + '秒后跳转登录页面',
					mask: true,
					duration: 1500
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-settle{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 50px;
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 14px;
		padding-left: 10px;
		.radio{
			display: flex;
			align-items: center;
		}
		.amount-box{
			.amount{
				color: #C00000;
				font-weight: bold;
			}
		}
		.btn-settle{
			background: #C00000;
			height: 50px;
			color: white;
			line-height: 50px;
			padding: 0 10px;
			min-width: 100px;
			text-align: center;
		}
	}
</style>
